# -*- coding: utf-8 -*-
"""
ECharts XY 轴图表基础组件
"""
from .echarts_base import TgEchartsBase
from pyecharts.charts.base import Base as EChartBase
import pandas as pd

class TgXYChartBase(TgEchartsBase):
    """ECharts XY 轴图表基础组件"""

    def __init__(self, parent=None):
        super().__init__(parent)
        self.x_axis_column = None
        self.y_axis_columns = []

    def set_data(self, df: pd.DataFrame, x_axis_column: str, y_axis_columns: list):
        """
        设置图表数据
        :param df: 数据DataFrame
        :param x_axis_column: X轴对应的数据列名
        :param y_axis_columns: Y轴对应的数据列名列表
        """
        self.df = df
        self.x_axis_column = x_axis_column
        self.y_axis_columns = y_axis_columns
        self.update_chart()

    def create_chart(self) -> EChartBase:
        """
        创建图表实例 (子类需要重写)
        """
        raise NotImplementedError("子类必须实现 create_chart 方法")

    def update_chart(self):
        """
        更新图表
        """
        if self.df is None or self.x_axis_column is None or not self.y_axis_columns:
            return

        chart = self.create_chart()
        chart.add_xaxis(self.df[self.x_axis_column].tolist())

        for col in self.y_axis_columns:
            chart.add_yaxis(col, self.df[col].tolist())

        self.render_chart(chart)